
package GUI;


import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Color;
import java.util.ArrayList;
import javax.swing.JComboBox;

import Entity.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;

/**
 * @author aviat
 *
 */

public class PassingManagerIncharge extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private JButton btnBack;
	private JLabel iconlabel;
	private JLabel lblPreviousManager;
	private JLabel lblNewManager;
	private JLabel lblDate;
	private JLabel lblMeetingId;
	private JComboBox<String> NewManagercomboBox;
	private JLabel lblMeetingDate;
	private JComboBox<String> mDateComboBox;
	private JComboBox<String> mTimeComboBox;
	private JButton btnApplay;
	
	private ArrayList<Meeting> m_allUsers;
	private ArrayList<Meeting> m_filteredUsers;
	private ArrayList<String> m_namesList;
	private Object syncObject;	
	private boolean m_isChangeAllowed;
	private JComboBox<String> eDatecomboBox;
	private Integer cID;
	private JButton btnRefresh;
	private JTextField prevManagerTextField;
	/**
	 * Constructor
	 */
	public PassingManagerIncharge() {
		setBackground(new Color(255, 51, 0));
		setLayout(null);
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(PassingManagerIncharge.class.getResource("/resources/back22.png")));
		NewManagercomboBox = new JComboBox<String>();
		eDatecomboBox = new JComboBox<String>();
		mDateComboBox = new JComboBox<String>();
		btnApplay = new JButton("Apply");
		btnRefresh = new JButton("refresh");
		mTimeComboBox = new JComboBox<String>();
		prevManagerTextField = new JTextField();
		
		initialize();
	}

	
	/**
	 * 
	 */
	private void initialize() {


		JLabel lblPassingManagerIncharge = new JLabel("Change Incharged Manager ");
		lblPassingManagerIncharge.setForeground(new Color(0, 204, 255));
		lblPassingManagerIncharge.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblPassingManagerIncharge.setBounds(10, 11, 272, 44);
		add(lblPassingManagerIncharge);
		
		btnBack.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnBack.setBounds(10, 462, 101, 27);
		add(btnBack);
		
		ImageIcon LOGOImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resources/camera-background3.png"));
		ImageIcon LOGO = new ImageIcon (LOGOImageIcon.getImage().getScaledInstance(-1, -1, java.awt.Image.SCALE_SMOOTH));
		
		lblPreviousManager = new JLabel("Previous Manager");
		lblPreviousManager.setForeground(Color.WHITE);
		lblPreviousManager.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblPreviousManager.setBounds(331, 98, 118, 27);
		add(lblPreviousManager);
		
		lblNewManager = new JLabel("New Manager");
		lblNewManager.setForeground(Color.WHITE);
		lblNewManager.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblNewManager.setBounds(331, 172, 92, 27);
		add(lblNewManager);
		
		lblDate = new JLabel("Event Date");
		lblDate.setForeground(Color.WHITE);
		lblDate.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblDate.setBounds(10, 262, 79, 27);
		add(lblDate);
		
		lblMeetingId = new JLabel("Meeting Date");
		lblMeetingId.setForeground(Color.WHITE);
		lblMeetingId.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblMeetingId.setBounds(10, 98, 101, 27);
		add(lblMeetingId);
		
		
		NewManagercomboBox.setBounds(450, 173, 132, 27);
		add(NewManagercomboBox);
		
		lblMeetingDate = new JLabel("Meeting Time");
		lblMeetingDate.setForeground(Color.WHITE);
		lblMeetingDate.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblMeetingDate.setBounds(10, 173, 92, 27);
		add(lblMeetingDate);
		
		
		mDateComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						filterList(mDateComboBox.getSelectedItem().toString(),"DATE");
				}
			}
		});
		mDateComboBox.setBounds(121, 99, 132, 27);
		add(mDateComboBox);
		
		
		mTimeComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						filterList(mTimeComboBox.getSelectedItem().toString(),"TIME");
				}
			}
		});
		mTimeComboBox.setBounds(121, 174, 132, 27);
		add(mTimeComboBox);
		
		
		eDatecomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						filterList(eDatecomboBox.getSelectedItem().toString(),"EDATE");
				}
			}
		});
		eDatecomboBox.setBounds(121, 264, 132, 27);
		add(eDatecomboBox);
		
		
		btnApplay.setIcon(new ImageIcon(PassingManagerIncharge.class.getResource("/resources/dialog_apply.png")));
		btnApplay.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnApplay.setBounds(356, 369, 118, 27);
		add(btnApplay);
		
		
		btnRefresh.setIcon(new ImageIcon(PassingManagerIncharge.class.getResource("/resources/arrow-refresh-2-icon.png")));
		btnRefresh.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						prevManagerTextField.setText("");
						filterList(null,"ALL");
				}
			}
		});
		btnRefresh.setBounds(193, 368, 118, 27);
		add(btnRefresh);

		prevManagerTextField.setFont(new Font("Tahoma", Font.BOLD, 12));
		prevManagerTextField.setEditable(false);
		prevManagerTextField.setBounds(450, 98, 132, 27);
		add(prevManagerTextField);
		
		iconlabel = new JLabel("");
		iconlabel.setIcon(LOGO);
		iconlabel.setBounds(0, 0, 786, 500);
		add(iconlabel);
	}
	
	/**
	 * @return the m_namesList
	 */
	public ArrayList<String> getM_namesList() {
		return m_namesList;
	}


	/**
	 * @param m_namesList the m_namesList to set
	 */
	public void setM_namesList(ArrayList<String> m_namesList) {
		System.out.println("size param : "+m_namesList.size());
		this.m_namesList = m_namesList;
		System.out.println("size this : "+this.m_namesList.size());
		
	}


	public void setAllMeeting(ArrayList<Meeting> allUsers) {
		m_allUsers = allUsers;		
		m_filteredUsers = new ArrayList<Meeting>();
		syncObject = new Object();
		filterList(null,"ALL");
	}
	
	private void filterList(String selectedItem, String scope)
	{
		synchronized (syncObject) {
			m_filteredUsers.clear();
			
			switch(scope)
			{
			case "EDATE":
				for(Meeting m : m_allUsers)
				{
					if(m.geteDate().equals(selectedItem))
					{
						m_filteredUsers.add(m);
					}
				}
				break;
			case "DATE":
				for(Meeting m : m_allUsers)
				{
					if(m.getmDate().equals(selectedItem))
					{
						m_filteredUsers.add(m);
					}
				}
				break;
			case "TIME":
				for(Meeting m : m_allUsers)
				{
					if(m.getmTime().equals(selectedItem))
					{
						m_filteredUsers.add(m);
					}
				}
				break;
			case "ALL":
				m_filteredUsers.addAll(m_allUsers);
				break;
			}
		}
		m_isChangeAllowed = false;
		filterComboBox(mDateComboBox,"DATE");
		filterComboBox(mTimeComboBox,"TIME");
		filterComboBox(eDatecomboBox,"EDATE");
			
		m_isChangeAllowed = true;

	}
	
	private void filterComboBox(JComboBox<String> combo, String scope)
	{
		combo.removeAllItems();
		synchronized (syncObject) {
			
			for(Meeting m : m_filteredUsers)
			{
				System.out.println("size: " +m_filteredUsers.size());
				switch(scope)
				{
				case "EDATE":
					System.out.println("in edate");
					combo.addItem(m.geteDate());
					break;
				case "DATE":
					combo.addItem(m.getmDate());
					break;
				case "TIME":
					combo.addItem(String.valueOf(m.getmTime()));
					break;
				}
				if(mDateComboBox.getItemCount()==1 && mTimeComboBox.getItemCount()==1 && eDatecomboBox.getItemCount()==1 ){
					prevManagerTextField.setText(m.getManagerIncharge());
					cID = m.getcID();
				}
			}
			NewManagercomboBox.removeAllItems();
				for(String name: m_namesList)
				{
					if(!(name.equals(prevManagerTextField.getText().toString())))
						NewManagercomboBox.addItem(name);
				}
			}

	}
	
	
	/**
	 * @return the cID
	 */
	public Integer getcID() {
		return cID;
	}


	/**
	 * @return the m_isChangeAllowed
	 */
	private boolean isChangeAllowed()
	{
		return m_isChangeAllowed;
	}
	/**
	 * @return the btnBack
	 */
	public JButton getBtnBack() {
		return btnBack;
	}


	/**
	 * @param btnBack the btnBack to set
	 */
	public void setBtnBack(JButton btnBack) {
		this.btnBack = btnBack;
	}


	/**
	 * @return the newManagercomboBox
	 */
	public JComboBox<String> getNewManagercomboBox() {
		return NewManagercomboBox;
	}


	/**
	 * @param newManagercomboBox the newManagercomboBox to set
	 */
	public void setNewManagercomboBox(JComboBox<String> newManagercomboBox) {
		NewManagercomboBox = newManagercomboBox;
	}



	/**
	 * @return the btnApplay
	 */
	public JButton getBtnApplay() {
		return btnApplay;
	}


	/**
	 * @return the eDatecomboBox
	 */
	public JComboBox<String> geteDatecomboBox() {
		return eDatecomboBox;
	}




	/**
	 * @return the mDateComboBox
	 */
	public JComboBox<String> getmDateComboBox() {
		return mDateComboBox;
	}


	/**
	 * @return the mTimeComboBox
	 */
	public JComboBox<String> getmTimeComboBox() {
		return mTimeComboBox;
	}


	/**
	 * @return the m_allUsers
	 */
	public ArrayList<Meeting> getM_allUsers() {
		return m_allUsers;
	}


	/**
	 * @param m_allUsers the m_allUsers to set
	 */
	public void setM_allUsers(ArrayList<Meeting> m_allUsers) {
		this.m_allUsers = m_allUsers;
	}


	/**
	 * @return the m_filteredUsers
	 */
	public ArrayList<Meeting> getM_filteredUsers() {
		return m_filteredUsers;
	}


	/**
	 * @param m_filteredUsers the m_filteredUsers to set
	 */
	public void setM_filteredUsers(ArrayList<Meeting> m_filteredUsers) {
		this.m_filteredUsers = m_filteredUsers;
	}


	/**
	 * @return the m_isChangeAllowed
	 */
	public boolean isM_isChangeAllowed() {
		return m_isChangeAllowed;
	}


	/**
	 * @param m_isChangeAllowed the m_isChangeAllowed to set
	 */
	public void setM_isChangeAllowed(boolean m_isChangeAllowed) {
		this.m_isChangeAllowed = m_isChangeAllowed;
	}


	/**
	 * @return the btnRefresh
	 */
	public JButton getBtnRefresh() {
		return btnRefresh;
	}


	/**
	 * @param btnRefresh the btnRefresh to set
	 */
	public void setBtnRefresh(JButton btnRefresh) {
		this.btnRefresh = btnRefresh;
	}


	/**
	 * @return the prevManagerTextField
	 */
	public JTextField getPrevManagerTextField() {
		return prevManagerTextField;
	}


	/**
	 * @param prevManagerTextField the prevManagerTextField to set
	 */
	public void setPrevManagerTextField(JTextField prevManagerTextField) {
		this.prevManagerTextField = prevManagerTextField;
	}


	/**
	 * @param mDateComboBox the mDateComboBox to set
	 */
	public void setmDateComboBox(JComboBox<String> mDateComboBox) {
		this.mDateComboBox = mDateComboBox;
	}


	/**
	 * @param mTimeComboBox the mTimeComboBox to set
	 */
	public void setmTimeComboBox(JComboBox<String> mTimeComboBox) {
		this.mTimeComboBox = mTimeComboBox;
	}


	/**
	 * @param btnApplay the btnApplay to set
	 */
	public void setBtnApplay(JButton btnApplay) {
		this.btnApplay = btnApplay;
	}


	/**
	 * @param eDatecomboBox the eDatecomboBox to set
	 */
	public void seteDatecomboBox(JComboBox<String> eDatecomboBox) {
		this.eDatecomboBox = eDatecomboBox;
	}
}
